<template>
  <!--App版本新增弹层start -->
  <el-dialog
    v-dialogDrag
    :title="dialogTitle"
    :close-on-click-modal="false"
    :visible.sync="dialogVisible"
    class="dialogStyleSmall"
    :before-close="cancel"
  >
    <el-form
      v-if="dialogVisible"
      ref="addForm"
      class="dialog-form"
      :model="formData"
      :rules="rules"
      label-width="80px"
    >
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="App版本" prop="versionNum">
            <el-input v-model="formData.versionNum" :readonly="readOnly" placeholder="请输入App版本号" size="medium" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="版本名称" prop="versionName">
            <el-input v-model="formData.versionName" placeholder="请输入App版本名称" size="medium" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="强制更新" prop="isRenew">
            <el-radio-group v-model="formData.isRenew">
              <el-radio label="0">是</el-radio>
              <el-radio label="1">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="版本信息" prop="versionInfo">
            <Remark :remark="formData.versionInfo" :rows="rows" :maxlength="maxlength" @getRemark="getRemark" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="安装文件" prop="updateAddressList">
            <FileUplodeOne
              ref="fileUplode"
              file-type=".apk"
              :edit-file-list="FileList"
              @getFileIdsList="getFileList"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <DialogFooter />
    </div>
  </el-dialog>
  <!--试验单位信息新增弹层end -->
</template>

<script>
// 页面接口
import { addMobileInfo, updateMobileInfo, getMobileInfoById } from '@/api/system/AppEdition/index'
// 附件上传
import FileUplodeOne from '@/components/Upload/FileUplodeOne'
// dialog-footer
import DialogFooter from '@/components/Common/Btns/DialogFooter/index'
// 备注
import Remark from '@/components/Common/Remark/index'
// 校验
import { integer } from '@/utils/validate'
export default {
  components: {
    FileUplodeOne,
    DialogFooter,
    Remark
  },
  data() {
    return {
      rows: 3,
      maxlength: 50,
      /* 新增 start*/
      readOnly: false,
      dialogTitle: '', // 弹层标题
      dialogVisible: false, // 弹层是否显示
      btnDisabled: false, // 确定按钮
      formData: { // 新增数据
        versionName: '', // App版本名
        versionNum: 0, // App版本
        isRenew: '1', // 强制更新
        versionInfo: '', // 版本信息
        updateAddressList: '', // App安装包
        stat: ''
      },
      // 表单验证规则
      rules: {
        versionName: [
          { required: true, message: 'App版本名不能为空', trigger: 'blur' },
          { required: true, max: 6, message: 'App版本号长度不得超过 30 个字符' }
        ],
        versionNum: [
          { required: true, message: 'App版本号不能为空', trigger: 'blur' },
          { validator: integer, trigger: 'blur' },
          { required: true, max: 6, message: 'App版本号长度不得超过 6 个字符' }
        ],
        isRenew: [
          { required: true, message: '是否强制更新', trigger: 'change' }
        ],
        versionInfo: [
          { required: true, message: '版本信息不能为空', trigger: 'blur' }
        ],
        updateAddressList: [
          { required: true, message: '请上传App安装包', trigger: 'blur' }
        ]
      },
      /* 新增 end*/
      FileList: []// 附件

    }
  },
  methods: {
    /* 新增/编辑弹层 start*/
    // 版本信息
    getRemark(remark) {
      this.formData.versionInfo = remark
    },
    // 新增/编辑
    handleAdd(title, id) {
      this.dialogTitle = title
      this.dialogVisible = true
      this.readOnly = false
      this.emptyAddForm() // 清空App版本
      if (id) {
        this.getDetailById(id)
        this.readOnly = true
      }
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false
      this.btnDisabled = false
      this.emptyAddForm()
    },
    // 提交按钮
    submitForm() {
      this.$refs['addForm'].validate(valid => {
        if (valid) {
          this.btnDisabled = true
          if (this.formData.stat === 'edit') {
            // 更新
            updateMobileInfo({ ...this.formData, versionNum: Number(this.formData.versionNum) }).then(res => {
              if (res) {
                this.cancel()
                this.msgSuccess('修改成功')
                this.$parent.getList()
              } else {
                this.msgError(res.message)
                this.btnDisabled = false
              }
            })
          } else {
            addMobileInfo({ ...this.formData, versionNum: Number(this.formData.versionNum) }).then(res => {
              if (res) {
                this.cancel()
                this.msgSuccess('保存成功')
                this.$parent.getList()
              } else {
                this.msgError(res.message)
                this.btnDisabled = false
              }
            })
          }
        }
      })
    },
    // 获取详情
    getDetailById(id) {
      getMobileInfoById(id).then(res => {
        if (res) {
          this.formData = { ...res.data }
          this.formData.stat = 'edit'
          this.formData.versionNum = res.data.versionNum + ''
          if (res.data.updateAddressList && res.data.updateAddressList.length > 0) {
            this.FileList = res.data.updateAddressList
          }
        }
      })
    },
    // 表单重置
    emptyAddForm() {
      this.resetForm('addForm')
      this.formData.stat = ''
      this.FileList = []
    },
    /* 新增弹层 end*/

    /** ************ 文件上传start *******************/
    // 接受文件上传 组件 回传过来的文件id
    getFileList(fileIdsList, deleteFileIds) {
      this.formData.updateAddressList = fileIdsList
    }
    /** ************ 文件上传end *******************/
  }

}
</script>

<style>

</style>
